# ==================================
#
#  Code for replicating:
# "Positioning Under Alternative Electoral Systems: Evidence From Japanese Candidate Election Manifestos"
#  Amy Catalinac, NYU
#
# ==================================


# ==================================
# Running Wordfish

install.packages("austin", repos="http://r-forge.r-project.org", type="source")
library(austin)

### 1986
data <- wfm("1986_end.csv") # see "preprocessing.R" for information on these files
wf <- wordfish(data)
theta <- wf$theta
theta <- as.data.frame(theta)
rownames(theta) <- wf$docs
ideal.points <- cbind(theta, wf$se.theta)
write.csv(ideal.points, file="1986_ideal_points.csv")
words <- coef(wf)
write.csv(words, file="1986_ideal_points_words.csv")

### 1990
data <- wfm("1990_end.csv")
wf <- wordfish(data)
theta <- wf$theta
theta <- as.data.frame(theta)
rownames(theta) <- wf$docs
ideal.points <- cbind(theta, wf$se.theta)
write.csv(ideal.points, file="1990_ideal_points.csv")
words <- coef(wf)
write.csv(words, file="1990_ideal_points_words.csv")

### 1993
data <- wfm("1993_end.csv")
wf <- wordfish(data)
theta <- wf$theta
theta <- as.data.frame(theta)
rownames(theta) <- wf$docs
ideal.points <- cbind(theta, wf$se.theta)
write.csv(ideal.points, file="1993_ideal_points.csv")
words <- coef(wf)
write.csv(words, file="1993_ideal_points_words.csv")

### 1996
data <- wfm("1996_end.csv")
wf <- wordfish(data)
theta <- wf$theta
theta <- as.data.frame(theta)
rownames(theta) <- wf$docs
ideal.points <- cbind(theta, wf$se.theta)
write.csv(ideal.points, file="1996_ideal_points.csv")
words <- coef(wf)
write.csv(words, file="1996_ideal_points_words.csv")

### 2000
data <- wfm("2000_end.csv")
wf <- wordfish(data)
theta <- wf$theta
theta <- as.data.frame(theta)
rownames(theta) <- wf$docs
ideal.points <- cbind(theta, wf$se.theta)
write.csv(ideal.points, file="2000_ideal_points.csv")
words <- coef(wf)
write.csv(words, file="2000_ideal_points_words.csv")

### 2003
data <- wfm("2003_end.csv")
wf <- wordfish(data)
theta <- wf$theta
theta <- as.data.frame(theta)
rownames(theta) <- wf$docs
ideal.points <- cbind(theta, wf$se.theta)
write.csv(ideal.points, file="2003_ideal_points.csv")
words <- coef(wf)
write.csv(words, file="2003_ideal_points_words.csv")

### 2005
data <- wfm("2005_end.csv")
wf <- wordfish(data)
theta <- wf$theta
theta <- as.data.frame(theta)
rownames(theta) <- wf$docs
ideal.points <- cbind(theta, wf$se.theta)
write.csv(ideal.points, file="2005_ideal_points.csv")
words <- coef(wf)
write.csv(words, file="2005_ideal_points_words.csv")

### 2009
data <- wfm("2009_end.csv")
wf <- wordfish(data)
theta <- wf$theta
theta <- as.data.frame(theta)
rownames(theta) <- wf$docs
ideal.points <- cbind(theta, wf$se.theta)
write.csv(ideal.points, file="2009_ideal_points.csv")
words <- coef(wf)
write.csv(words, file="2009_ideal_points_words.csv")

rm(list=ls())

# NB: Which end represented the ideological left and right varied across election.  
# To determine which end was which, we made an initial determination by using these .csv files
# to identify manifestos at the extremes in each election.  Then, we read these manifestos.
# To make lower numbers depict the left in all eight elections, we multiplied
# the theta scores in 1986, 1993, 2000, 2005, and 2009 by -1 (in these elections, lower numbers 
# represented the right).  Then we merged the ideal points for each election into one file,
# and merged this with covariate data from Reed's 2007 and 2011 MMD and SMD data sets.
# We saved this file as: "covars_ideal_points.Rdata"

# In doing so, we made a new "ku" variable by combining Reed's "mmd.ku" and "smd.ku" variables, 
# and a new "pty" variable by combining Reed's "mmd.pty" and the "smd.Pty�h variables.
# "Catalinac_party_var_codebook.doc" lists the codes for Reed's original party variables, 
# and the codes for our "pty" variable.



